package com.yantong.service.impl;

import com.yantong.pojo.IndustryBrand;
import com.yantong.mapper.IndustryBrandMapper;
import com.yantong.service.IndustryBrandService;
import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 *  请填写IndustryBrandServiceImpl的描述
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 16:47
 */
@Service
public class IndustryBrandServiceImpl implements IndustryBrandService {

	@Autowired
	private IndustryBrandMapper industryBrandMapper;

	@Override
	public int saveIndustryBrand(IndustryBrand industryBrand) {
		if (industryBrand == null) {
			return 0;
		}
		IndustryBrand industryBrandOld = industryBrandMapper.selectByPrimaryKey(industryBrand.getId());
		if (industryBrandOld != null) {
			throw new BDException(BusinessCode.CENTRE_TERMINAL_EXIST);
		}
		return industryBrandMapper.insertSelective(industryBrand);
	}

	@Override
	public PageUtils<IndustryBrand> listIndustryBrand(IndustryBrand industryBrand, int offset, int limit) {
		PageUtils<IndustryBrand> pageUtils = new PageUtils<>();
		int total = industryBrandMapper.countByAll(industryBrand);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(industryBrandMapper.listByAll(industryBrand,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateIndustryBrand(IndustryBrand industryBrand) {
		if (industryBrand == null || industryBrand.getId() == null) {
			return 0;
		}
		return industryBrandMapper.updateByPrimaryKeySelective(industryBrand);
	}

	@Override
	public IndustryBrand getIndustryBrandById(Long id) {
		if (id == null) {
			return null;
		}
		return industryBrandMapper.selectByPrimaryKey(id);
	}

	@Override
	public int removeProductGrade(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return 0;
		}
		return industryBrandMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<IndustryBrand> listByCentreId(HashMap hashMap) {
		return industryBrandMapper.listByCentreId(hashMap);
	}
}
